package com.quasar.hpatient.module.comm_album_folder;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.quasar.hpatient.R;
import com.quasar.hpatient.bean.comm_album.AlbumFolderBean;
import com.quasar.hpatient.widget.StateView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import lib.quasar.base.frame.BasePresenter;
import lib.quasar.recycler.BaseCommonAdapter;
import lib.quasar.recycler.holder.RecyclerHolder;
import lib.quasar.recycler.manager.CrashLinearLayoutManager;

/**
 * description: 选择图片, 相册集合
 * created by kalu on 2018/4/18 15:53
 */
public class AlbumFolderPresenter implements BasePresenter {

    private final ArrayList<AlbumFolderBean> mData = new ArrayList<>();

    void initLayoutData(final Context context, final AlbumFolderView view) {

        if (null == view || null == context) return;

        // 适配器
        final BaseCommonAdapter<AlbumFolderBean> adapter = new BaseCommonAdapter<AlbumFolderBean>(mData, R.layout.fragment_comm_album_folder_child) {

            @Override
            protected void onNext(RecyclerHolder holder, final AlbumFolderBean model, int position) {

                ImageView image = holder.getView(R.id.fragment_comm_album_folder_child_image);
                view.setImage(image, model.getFolderImage());
                view.setText(holder, R.id.fragment_comm_album_folder_child_name, model.getFolderName());
                view.setText(holder, R.id.fragment_comm_album_folder_child_count, model.getImageList().size() + context.getResources().getString(R.string.comm_album_unit));
                view.setVisibility(holder, R.id.fragment_comm_album_folder_child_check, model.isSelect() ? View.VISIBLE : View.GONE);

                holder.setOnClickListener(R.id.fragment_comm_album_folder_child, (v) -> {
                    for (int i = 0; i < mData.size(); i++) {
                        AlbumFolderBean temp = mData.get(i);
                        temp.setSelect(temp.getFolderName().endsWith(model.getFolderName()));
                    }
                    view.setRecycler(true, null, null, null);
                    view.switchFolder(model.getFolderName());
                });
            }
        };

        StateView loading = (StateView) LayoutInflater.from(context).inflate(R.layout.layout_recycler_loading, null);
        loading.setState(StateView.STATE_LOADING);
        adapter.setNullView(loading);

        final CrashLinearLayoutManager manager = new CrashLinearLayoutManager(context);
        view.setRecycler(false, adapter, manager, null);
    }

    void insertPhoto(final AlbumFolderView view, Map<String, AlbumFolderBean> map) {

        if (null == view || null == map || map.size() == 0) return;

        for (HashMap.Entry<String, AlbumFolderBean> entry : map.entrySet()) {
            mData.add(entry.getValue());
        }
        view.setRecycler(true, null, null, null);
    }

    @Override
    public void recycler() {
    }
}
